//
//  CustomLayout.m
//  适配之道
//
//  Created by macbook on 2017/5/23.
//  Copyright © 2017年 oceansoft. All rights reserved.
//

#import "CustomLayout.h"

@implementation CustomLayout


-(void)prepareLayout
{
    
}

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray* attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    NSLog(@"%@",self.collectionView.layoutGuides);
    
    for(int i = 0; i < [attributes count]; ++i) {
        
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[i];

        if (currentLayoutAttributes.indexPath.section == 0) {
            if (i == 0) {
                
                CGRect frame = currentLayoutAttributes.frame;
                
                frame.origin.x = 0;
                
                //            frame.size.width = [UIScreen mainScreen].bounds.size.width - frame.origin.x;
                
                currentLayoutAttributes.frame = frame;
                
                
            }else{
                
                UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[i - 1];
                
                // 1/[UIScreen mainScreen].scale
                CGFloat maximumSpacing = KLineWidth;
                
                CGFloat origin = CGRectGetMaxX(prevLayoutAttributes.frame);
                
                if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
                    
                    if(i == 1){
                        CGRect frame = currentLayoutAttributes.frame;
                        
                        frame.origin.x = origin + maximumSpacing;
                        
                        frame.size.width = [UIScreen mainScreen].bounds.size.width - frame.origin.x;
                        
                        currentLayoutAttributes.frame = frame;
                        
                    }else{
                        CGRect frame = currentLayoutAttributes.frame;
                        
                        frame.origin.x = origin + maximumSpacing;
                        
                        currentLayoutAttributes.frame = frame;
                    }
                }
                else{
                    if (i != 2) {
                        
                        if(i == 1){
                            CGRect frame = currentLayoutAttributes.frame;
                            
                            frame.origin.x = origin + maximumSpacing;
                            
                            frame.size.width = [UIScreen mainScreen].bounds.size.width - frame.origin.x;
                            
                            currentLayoutAttributes.frame = frame;
                            
                        }else{
                            CGRect frame = currentLayoutAttributes.frame;
                            
                            frame.origin.x = origin + maximumSpacing;
                            
                            currentLayoutAttributes.frame = frame;
                        }
                    }else{
                    }
                }
            }
        }
        

    }
    
    for (int i = 0; i<attributes.count ; i++) {
        
        UICollectionViewLayoutAttributes *attr = attributes[i];
        
        NSLog(@"%d ---- %@", i, NSStringFromCGRect([attr frame]));
    }
    
    return attributes;
}

@end
